package net.pws.oos.web;

import java.util.Date;
import java.util.List;

import net.pws.common.jackson.JsonUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.TaskTemplate;
import net.pws.oos.biz.query.TaskTemplateFinder;
import net.pws.oos.biz.query.parameter.TaskTemplateQueryParameter;
import net.pws.oos.biz.service.TaskTemplateService;
import net.pws.oos.web.dto.TaskTemplateDto;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/biz/task/template")
public class TaskTemplateAction {

	private Log logger = LogFactory.getLog(TaskTemplateAction.class);

	@Autowired
	private EntityManager entityManager;

	@Autowired
	private TaskTemplateFinder taskTemplateFinder;

	@Autowired
	private TaskTemplateService taskTemplateService;

	@ResponseBody
	@RequestMapping("/save.json")
	public Object saveTemplate(TaskTemplateDto dto) {
		try {
			TaskTemplate model = null;
			if (StringUtils.isNotBlank(dto.getId())) {
				model = entityManager.find(TaskTemplate.class, dto.getId());
			} else {
				model = new TaskTemplate();
				model.setCreateDate(new Date());
			}
			TaskTemplateDto.to(model, dto, entityManager);
			entityManager.save(model);
			return JsonUtils.succeedMap(TaskTemplateDto.from(model));
		} catch (Exception e) {
			logger.error(e, e);
			return JsonUtils.failedJson(e.getMessage());
		}
	}

	@RequestMapping("/list.do")
	public String listPage() {
		return "task/template/list";
	}

	@ResponseBody
	@RequestMapping("/list.json")
	public Object list(TaskTemplateQueryParameter parameter) {
		try {
			List<TaskTemplate> models = taskTemplateFinder.listTop(parameter);
			List<TaskTemplateDto> dtos = TaskTemplateDto.from(models);
			// TODO 获得子模板数量
			return JsonUtils.succeedMap(dtos);
		} catch (Exception e) {
			logger.error(e, e);
			return JsonUtils.failedJson(e.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/count.json")
	public Object count(TaskTemplateQueryParameter parameter) {
		try {
			int count = taskTemplateFinder.countTop(parameter);
			return JsonUtils.succeedMap(count);
		} catch (Exception e) {
			logger.error(e, e);
			return JsonUtils.failedJson(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/detail.json")
	public Object detail(String id) {
		try {
			TaskTemplate model = entityManager.find(TaskTemplate.class, id);
			return JsonUtils.succeedMap(TaskTemplateDto.from(model));
		} catch (Exception e) {
			logger.error(e, e);
			return JsonUtils.failedJson(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/children.json")
	public Object listChildren(String id) {
		try {
			List<TaskTemplate> children = taskTemplateFinder.findChildren(id);
			return JsonUtils.succeedMap(TaskTemplateDto.from(children));
		} catch (Exception e) {
			logger.error(e, e);
			return JsonUtils.failedJson(e.getMessage());
		}
	}

	@RequestMapping("/edit.do")
	public String editPage(String id) {
		if (StringUtils.isBlank(id)) {
			return "task/template/new";
		} else {
			return "task/template/edit";
		}
	}

}
